<?php


namespace app\user\controller;


use think\Controller;
use think\Db;
use think\facade\Request;
use think\facade\Validate;

class Demand extends Controller
{
    protected $middleware = [
        'app\http\middleware\checkTTUserToken::class'
    ];

    //添加需求
    public function add()
    {
        $data=Request::only(['user_id','name','mobile','desc']);

        $rule=[
            "name|名字"=>"require|max:5",
            "mobile|手机号"=>"require|mobile",
            "desc|描述"=>"require|max:200",
        ];

        //验证表单
        $validate = Validate::make($rule);

        if (!$validate->check($data)) {
            $this->result('',400,$validate->getError());
        }


        //验证是否重复提交
        $count=Db::table('demand')->where([
            [
                'user_id','=',$data['user_id']
            ],
            [
                'add_time','BETWEEN',today()
            ]
        ])->field('id')->count('id');


        //一天只能提交三次
        if($count>=3){
            $this->result(null,400,'一天只能提交3次');
        }

        //添加需求
        $data['add_time']=time();
        $res=Db::table('demand')->insert($data);

        if($res){
            $this->result(null,200,'添加成功');
        }else{
            $this->result(null,400,'提交需求失败,请重试');
        }


    }


}
